﻿    
    /*
    设备
    */



    /*
      刷新设备
    */
    function JSTest_Device_Refresh()
	{    
		
		 
			 deviceSelect.options.length=0;     
		     var NETCAPKI_DEVICETYPE_ANY = -1;
             var NETCAPKI_DEVICEFLAG_CACHE_PIN_IN_PROCESS = 0;
             var device;
		     localdeviceSetObject=NetcaPKI._baseObject.CreateDeviceSetObject();
	         localdeviceSetObject.GetAllDevice(NETCAPKI_DEVICETYPE_ANY,NETCAPKI_DEVICEFLAG_CACHE_PIN_IN_PROCESS);   
		     var count = localdeviceSetObject.Count;
			 if(count<=0)
			 {
				alert("获取设备集失败!");
				return;
			 }
             
             for( var index = 1; index <= count; ++index)
             {
               device = localdeviceSetObject.Item(index);
			   
			   
			   var oOption = document.createElement("OPTION");
			   oOption.text=device.SerialNumber!=""?device.SerialNumber:device.Label;
			   oOption.value=index;
			   deviceSelect.add(oOption);
               
            }
	 
		
	}
    
    /*
    验证设备密码
    */
    
    
    function JSTest_Device_VerifyPassword()
	{
	  
		 

        if(localdeviceSetObject==null)
        {
            alert("先刷新设备!");
            return;
        }
        var index=deviceSelect.selectedIndex; //序号，取当前选中选项的序号
        var val = deviceSelect.options[index].value;

        var device = localdeviceSetObject.Item(parseInt(val));
        
        if(device==null)
        {
            alert("获取设备失败!");
            return;
        }
        
        /*
        验证设备密码使用 deviceObject对象的VerifyPwd方法，参数是密码，密码类型
        */
        
        var bOk= device.VerifyPwd(devicePassword.value,NetcaPKI.USER_PWD);
        
        if(bOk)
        {
            alert("验证用户密码成功!");
        }
        else
        {
            alert("验证用户密码失败!");
        }
		  
	}
    	
     /*
         修改用户密码
     */
	 function JSTest_Device_ModifyPassword()
	 {
		 
		  

            if(localdeviceSetObject==null)
            {
                alert("先刷新设备!");
                return;
            }
            var index=deviceSelect.selectedIndex; //序号，取当前选中选项的序号
            var val = deviceSelect.options[index].value;
    
            var device = localdeviceSetObject.Item(parseInt(val));
            
            if(device==null)
            {
                alert("获取设备失败!");
                return;
            }
            
            /*
            修改用户密码使用 deviceObject对象的ChangePwd方法，参数是新旧密码和密码类型
            */
            
            var bOk= device.ChangePwd(olddevicePassword.value,modifydevicePassword.value,NetcaPKI.USER_PWD);
            if(bOk)
            {
                alert("修改密码成功!");
            }
            else
            {
                alert("修改密码失败!");
            }
	 
	 
		 
	 }
    
     /*
         读取设备数据
     */
    function JSTest_Device_ReadData()
    {
         
         if(localdeviceSetObject==null)
         {
                alert("先刷新设备!");
                return;
         }
         
         var index=deviceSelect.selectedIndex; //序号，取当前选中选项的序号
         var val = deviceSelect.options[index].value;
 
         var device = localdeviceSetObject.Item(parseInt(val));
         
         if(device==null)
         {
                alert("获取设备失败!");
                return;
         }

        /*
          读取设备文件，使用device 的ReadData方法
        */
        var byteData= device.ReadData(parseInt(devicedataid.value));
        
        if(NetcaPKI.getLastError()!=NetcaPKI.OK)
        {
                alert(NetcaPKI.getLastErrorMsg());
                return;
        } 
        
        deviceDatafile.value=NetcaPKI.newString(byteData)
         
     }

       function JSTest_Device_WriteData()
      {
 

                 if(localdeviceSetObject==null)
                 {
                        alert("先刷新设备!");
                        return;
                 }
                 
                 var index=deviceSelect.selectedIndex; //序号，取当前选中选项的序号
                 var val = deviceSelect.options[index].value;
         
                 var device = localdeviceSetObject.Item(parseInt(val));
                 
                 if(device==null)
                 {
                        alert("获取设备失败!");
                        return;
                 }
                 //创建data
                    
                //转换UTF8编码 stringEncode缺省使用UTF8
                //写文件需要先创建文件，在写文件
                var utf8clearText=NetcaPKI.stringEncode(deviceDatafile.value); 
                var dataLen=NetcaPKI._baseObject.GetByteArrayLength(utf8clearText);
                device.DeleteData(parseInt(devicedataid.value));

                device.CreateData(parseInt(devicedataid.value),"04070707",dataLen);
                if(NetcaPKI.getLastError()!=NetcaPKI.OK)
                {
                       /* if(-2147064743==NetcaPKI.getLastError())
                        {

                        }
                        else
                        {*/
                            alert(NetcaPKI.getLastErrorMsg());
                            return;
                       // }
                } 
                device.WriteData(parseInt(devicedataid.value),utf8clearText);
                
                if(NetcaPKI.getLastError()!=NetcaPKI.OK)
                {
                        alert(NetcaPKI.getLastErrorMsg());
                        return;
                } 
                else
                {
                        alert("写数据成功!");
                        return;
                }
            
      }
         
     